package com.panshi.controller.information;

import com.panshi.DiscussPage.PageBeanVo;
import com.panshi.api.ArticleApi;
import com.panshi.api.ISeekApi;
import com.panshi.api.QuestionApi;
import com.panshi.api.SortApi;
import com.panshi.po.SeekPO;
import com.panshi.po.SortPO;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@Slf4j
@RequestMapping("/message")
public class ZXMessageController {

    @Resource
    private ISeekApi iSeekApi;

    @Resource
    private SortApi sortApi;

    @Resource
    private ArticleApi articleApi;

    @Resource
    private QuestionApi questionApi;

    @GetMapping("/query")
    public String queryPage(HttpServletRequest request){

        List<SortPO> sortPOList = sortApi.totalQuery().getData();

        request.setAttribute("sortList",sortPOList);

        return "information/zxmessage/message-query";
    }

    @PostMapping("/query")
    @ResponseBody
    public PageModel<SeekPO> query(PageModel<SeekPO> pm){
        pm.init();

        PageBeanSeekVo pageBeanVo = new PageBeanSeekVo();
        pageBeanVo.setCurrentPage(pm.getCurrentPage());
        pageBeanVo.setPageSize(pm.getPageSize());

        List<SeekPO> list = iSeekApi.querySeekPOPostBody(pageBeanVo).getData();
        log.info("资讯信息query ----------------list{}",list);
        pm.setList(list);

        Integer count = iSeekApi.queryTotalSeekPOPostBody().getData();
        pm.setCount(count);

        return pm;
    }

    @PostMapping("/query-vague/{vague}")
    @ResponseBody
    public PageModel<SeekPO> queryVague(PageModel<SeekPO> pm, @PathVariable String vague){
        pm.init();
        log.info("pm ------------------------{}",pm);

        SeekQueryDimVO seekQueryDimVO = new SeekQueryDimVO();
        seekQueryDimVO.setTitle(vague);
        seekQueryDimVO.setCurrentPage(pm.getCurrentPage());
        seekQueryDimVO.setPageSize(pm.getPageSize());

        List<SeekPO> sortPOList = iSeekApi.queryDimSeekPOPostBody(seekQueryDimVO).getData();
        pm.setList(sortPOList);

        Integer count = iSeekApi.queryNumberSeekPOPostBody(vague).getData();
        pm.setCount(count);

        return pm;
    }

    @PostMapping("/query-sortname/{sortname}")
    @ResponseBody
    public PageModel<SeekPO> queryClassify(PageModel<SeekPO> pm, @PathVariable String sortname){
        pm.init();
        log.info("pm ------------------------{}",pm);

        QuerySortnameVO querySortnameVO = new QuerySortnameVO();
        querySortnameVO.setSortname(sortname);
        querySortnameVO.setCurrentPage(pm.getCurrentPage());
        querySortnameVO.setPageSize(pm.getPageSize());

        List<SeekPO> sortPOList = iSeekApi.querySortnameSeekPOPostBody(querySortnameVO).getData();
        pm.setList(sortPOList);

        Integer count = iSeekApi.querySortnameNumberSeekPOPostBody(sortname).getData();
        pm.setCount(count);

        return pm;
    }

    @PostMapping("/query-state/{state}")
    @ResponseBody
    public PageModel<SeekPO> queryState(PageModel<SeekPO> pm, @PathVariable Integer state){
        pm.init();
        log.info("pm ------------------------{}",pm);
        log.info("state ------------------------{}",state);
        QueryStateVO queryStateVO = new QueryStateVO();
        queryStateVO.setState(state);
        queryStateVO.setCurrentPage(pm.getCurrentPage());
        queryStateVO.setPageSize(pm.getPageSize());


        List<SeekPO> sortPOList = iSeekApi.queryStateSeekPOPostBody(queryStateVO).getData();
        pm.setList(sortPOList);

        log.info("sortPOList ------------------------{}",sortPOList);

        Integer count = iSeekApi.querystateNumberSeekPOPostBody(state).getData();
        pm.setCount(count);

        return pm;
    }

    @GetMapping("/add")
    public String add(Model model){
        List<SortPO> sortPOList = sortApi.totalQuery().getData();

        model.addAttribute("sortList",sortPOList);

        return "information/zxmessage/message-add";
    }

    @PostMapping("/add")
    @ResponseBody
    public PageModel<SeekPO> add(AddSeekVO addSeekVO){
        PageModel<SeekPO> pageModel = new PageModel<>();
        String number = UUID.randomUUID().toString().replaceAll("-","");
        addSeekVO.setNumber(number);
        addSeekVO.setCreatedate(new Date());
        addSeekVO.setPublishdate(new Date());

        log.info("addSeekVO ====================== {}",addSeekVO);

        Integer state =iSeekApi.addSeekPOPostBody(addSeekVO).getData();
        log.info("state =============================== {}",state);
        if(state == 0){
            pageModel.setCode(9999);
            pageModel.setMsg("新增失败");
        }else if(state == 1){
            /*if(addSeekVO.getTypees() == 0){//文章
                ArticleAddVo articleAddVo = new ArticleAddVo();
                articleAddVo.setContent(addSeekVO.getContent());
                articleAddVo.setCreatedate(addSeekVO.getCreatedate());
                articleAddVo.setDeliverdate(new Date());
                articleAddVo.setImgpath(addSeekVO.getImgpath());
                articleAddVo.setIntro(addSeekVO.getIntro());
                articleAddVo.setKeywords(addSeekVO.getKeyword());
                articleAddVo.setNumber(addSeekVO.getNumber());
                articleAddVo.setSortname(addSeekVO.getSortname());
                articleAddVo.setTitle(addSeekVO.getTitle());
                articleAddVo.setUid(addSeekVO.getUid());
                articleApi.addArticle(articleAddVo);
            }else if(addSeekVO.getTypees() == 1){//问题
                QuestionAddVO questionAddVO = new QuestionAddVO();
                questionAddVO.setSortname(addSeekVO.getSortname());
                questionAddVO.setQuestion(addSeekVO.getTitle());
                questionAddVO.setCreatedate(addSeekVO.getCreatedate());
                questionAddVO.setBackdrop(addSeekVO.getBackground());
                questionAddVO.setDeliverdate(new Date());
                questionAddVO.setNumber(addSeekVO.getNumber());
                questionAddVO.setUid(addSeekVO.getUid());
                questionApi.questionAdd(questionAddVO);
            }*/
            pageModel.setCode(10000);
            pageModel.setMsg("新增成功");
        }
        return pageModel;

    }

    @GetMapping("/edit")
    public String edit(@RequestParam Integer id, Model model){

        List<SortPO> sortPOList = sortApi.totalQuery().getData();

        model.addAttribute("sortList",sortPOList);

        SeekPO seekPO = iSeekApi.queryIdSeekPOPostBody(id).getData();

        model.addAttribute("seek",seekPO);

        return "information/zxmessage/message-edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public PageModel<SeekPO> edit(UpdateSeekVO updateSeekVO){
        PageModel<SeekPO> pageModel = new PageModel<>();
        log.info("updateSeekVO ====================== {}",updateSeekVO);
        updateSeekVO.setUpdatedate(new Date());
        Integer status = iSeekApi.updateSeekPOPostBody(updateSeekVO).getData();
        if(status == 0){
            pageModel.setCode(9999);
            pageModel.setMsg("修改失败");
        }else{
            pageModel.setCode(10000);
            pageModel.setMsg("修改成功");
        }
        return pageModel;
    }

    @PostMapping("/del")
    @ResponseBody
    public PageModel<SeekPO> del(DeleteSeekVO deleteSeekVO){
        PageModel<SeekPO> pageModel = new PageModel<>();
        log.info("updateSeekVO ====================== {}",deleteSeekVO);
        Integer status = iSeekApi.deleteSeekPOPostBody(deleteSeekVO).getData();
        if(status == 0){
            pageModel.setCode(9999);
            pageModel.setMsg("删除失败");
        }else{
            pageModel.setCode(10000);
            pageModel.setMsg("删除成功");
        }
        return pageModel;
    }

}
